import COM.CCB.EnDecryptAlgorithm.MCipherEncryptor;
import ccb.pay.api.util.CCBPayUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xiexiaojin
 * @date 2024/3/22-14:05
 * @description
 */
public class Test {

    /***
    *广州南方番禺附属学校参数
    */
    private static String qrcode = "CCBXYh2B1kC7oOz7BxU1wFwrOZ/NNVD45OIM3/a/v7ZzSyeGR7b38KpYd/v/H44YNxjRWWXISDy7jxyiNHkw4VM9kfIeFsGNNF5nos6jO/nSmJ4kcqRFsbU8JV13FsjftH6IlGXww2L/v78DdWhytcBwl7A==";
    private static String shl_id = "temp6982091";
    private static String ECARDEVID = "100001";
    private static String st_id="W202403019";
    private static String BRANCHID = "441000000";
    private static String MERCHANTID = "105000582997802";
    private static String POSID = "072564149";
    private static String TrdPt_Pltfrm_OrCd = "8396";
    private static String MERFLAG="1";
    private static String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

    public static void main(String[] args) throws Exception {
        encodeUrlTwo();
    }


    public static void encodeUrlTwo() throws Exception{
        String sc = "https://app.xiaoyuan.ccb.com";//生产环境
        //修改了学校，记得把strKey，ECARDEVID也改成对应学校的值
        String strKey = "dukZFRXOGXaJ4BViue9YpHbpGRUVzhl2";  //广州南方学院番禺附属中学一卡通密钥
//        String strKey = "YuC8irDEhqKnFXUZlEm6jGLgvIqwxIai";   //广州商学院一卡通密钥
        //记得对应修改ECARDEVID
        String head_url = sc+"/LHECISM/CCBYXServiceReqServlet?ECARDEVID="+ECARDEVID+"&SCHID="+shl_id+"&ccbParam=";

        String AMOUNT="0.01";  //交易金额
        //加密
        String ccbParm = yxy906(AMOUNT); //YXY300解码
//        String ccbParm = W019(); //W019
//        String ccbParm = yxy922(AMOUNT);  //退款（需修改退款金额和订单号）
//        String ccbParm = yxy928(AMOUNT);  //离线码支付
        MCipherEncryptor ccbEncryptor = new MCipherEncryptor(strKey.substring(strKey.length() - 30));
        ccbParm = ccbEncryptor.doEncrypt(ccbParm);
        System.out.println(head_url+ccbParm);

        //PAY300需要YXY300解码才可以执行
//        testPay300(AMOUNT);

        //测试PAY400需要YXY300解码才可以执行
//        String ccbParm2 = PAY400(AMOUNT);
//        ccbParm2 = ccbEncryptor.doEncrypt(ccbParm2);
//        System.out.println(head_url+ccbParm2);

    }
    //YXY906 PAYCONTENT是mer_info表中的term_id字段
    public static String yxy906(String AMOUNT){
        System.out.println("YXY906");
        String ordr_id="TESTYXY906"+date;
        String ccbParm = "TXCODE=YXY906&BRANCHID="+BRANCHID+"&SERVLET_NAME=B2CMainPlat_00&CCB_IBSVersion=V6&PT_STYLE=10"
                +"&ST_ID="+st_id+ "&SHL_ID="+shl_id+"&AMOUNT="+AMOUNT+"&ORDERID="+ordr_id+"&TERMNO1=2029323233232"
                +"&CST_NM=肖琳&PAYCONTENT=termxie01";
        return ccbParm;
    }
    //退款，退款需提供订单号ordr_id和金额
    public static String yxy922(String money){
        System.out.println("YXY922");
        String ordr_id="24032110050766759020";//修改订单号进行退款
        String ccbParm = "TXCODE=YXY922&BRANCHID="+BRANCHID+"&SERVLET_NAME=B2CMainPlat_00&CCB_IBSVersion=V6&PT_STYLE=10"
                + "&Shl_id="+shl_id+"&MERCHANTID="+MERCHANTID+"&MONEY="+money+"&ORDER="+ordr_id+"&TERMNO=";
        return ccbParm;
    }
    //YXY300解码
    public static String yxy300(String AMOUNT){
        System.out.println("yxy300");
        String ordr_id="TESTYXY300"+date;
        String ccbParm = "TXCODE=YXY300&BRANCHID="+BRANCHID+"&PT_STYLE=10&"
                + "&TERMNO=01&SHL_ID="+shl_id+"&AMOUNT="+AMOUNT+"&QRCODE="+qrcode
                + "&MCH_ID="+MERCHANTID+"&QRCODETYPE=5&ORDERID="+ordr_id;
        return ccbParm;
    }
    //PAY400盒子支付
    public static String PAY400(String AMOUNT){
        System.out.println("PAY400");
        String order_id="TESTPAY400"+date;
        String ccbParm = "TXCODE=YXY800&BRANCHID="+BRANCHID+"&SERVLET_NAME=B2CMainPlat_00&CCB_IBSVersion=V6&PT_STYLE=10"
                + "&MERCHANTID="+MERCHANTID+"&POSID="+POSID+"&MERFLAG="+MERFLAG+"&AMOUNT="+AMOUNT+"&TrdPt_Pltfrm_OrCd="+TrdPt_Pltfrm_OrCd
                + "&Device_Info=2029323233232&SHL_ID="+shl_id+"&ST_ID="+st_id+"&REMARK1=123"
                + "&ORDERID="+order_id+"&QRCODE="+qrcode;
        return ccbParm;
    }

    //PAY300
    public static void testPay300(String AMOUNT) throws Exception{
        //银行接口url
        String host = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_BEPAY?";//生产
//        String host = "http://128.128.97.200:8102/CCBIS/B2CMainPlat_00_BEPAY?";//测试

        //商户信息
        String merInfo = "MERCHANTID="+MERCHANTID+"&POSID="+POSID+"&BRANCHID="+BRANCHID;
        //获取柜**整公钥
        String pubKey = "30819c300d06092a864886f70d010101050003818a003081860281805fb1c81d5fe94af64b12fbe8d937ce9b0ae55c07270625dde05b1a5601d2c3769e25bddc62a8ff31fa91e31d22d0d617612adf22c5044d431e93564e42905b8fc3f840b91ec04d45e27c193d8c24086521e731d12905962024bbf529894cec7b872f61d69a492ff543aeb1f76699bcb9640545248f86182fe3029a56808b238f020111";
        String ordr_id="TESTPAY300"+date;
        String param = merInfo + "&MERFLAG="+MERFLAG+"&TERMNO1=&TERMNO2=&ORDERID=" +ordr_id+
                "&QRCODE="+qrcode+"&AMOUNT="+AMOUNT+"&TXCODE=PAY300&PROINFO=&REMARK1=&REMARK2=&TrdPt_Pltfrm_OrCd="+TrdPt_Pltfrm_OrCd;
        System.out.println("PAY300加密原串="+param);
        //执行加密操作
        CCBPayUtil ccbPayUtil = new CCBPayUtil();
        String url = ccbPayUtil.makeCCBParam(param, pubKey);
        //拼接请求串
        url = host + merInfo + "&ccbParam=" + url;
        System.out.println(url);


    }

    //离线码
    public static String yxy928(String AMOUNT) throws UnsupportedEncodingException {
        //学号@客户签名@慧学签名@时间戳
        String ordr_id="TESTYXY928"+date;
        String ccbParm = "TXCODE=YXY928&BRANCHID="+BRANCHID+"&SERVLET_NAME=B2CMainPlat_00&CCB_IBSVersion=V6&PT_STYLE=10"
                + "&SHL_ID="+shl_id+"&Device_Info=105910000004322&QRCODE="+ URLEncoder.encode(qrcode, "UTF-8")+"&MERCHANTID="+MERCHANTID
                + "&MERFLAG="+MERFLAG+"&POSID="+POSID+"&ORDERID="+ordr_id+"&AMOUNT="+AMOUNT+"&TrdPt_Pltfrm_OrCd="+TrdPt_Pltfrm_OrCd
                + "&ST_ID="+st_id;
        return ccbParm;
    }

    public static String W019(){
        System.out.println("W019");
        String TXCODE = "YXY801";
        String ST_ID   = "W202403019";//学工号
        String TERM_ID = "termxie01";//终端ID
        String ORDERID = "TESTW019"+date;//订单号
        String AMOUNT  = "0.01";//订单金额
        String Crdt_No = "360721199811158024";//身份证号

        String ccbParm ="TXCODE="+TXCODE+"&BRANCHID="+BRANCHID+"&SHL_ID="+shl_id+"&ST_ID="+ST_ID+
                "&TERM_ID="+TERM_ID+"&ORDERID="+ORDERID+"&AMOUNT="+AMOUNT+"&Crdt_No="+Crdt_No;
        return ccbParm;
    }

}
